<?php
if(get_magic_quotes_gpc()){ die('Magic Quotes activated.'); }

ob_start();
session_start();
header('Content-type: text/html; charset=UTF-8');

/* Manage language session */
if(!isset($_COOKIE['lang']) || !isset($_SESSION['lang'])){
	$_SESSION['lang'] = substr(strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']), 0, 2);
}

error_reporting(E_ALL);
date_default_timezone_set('Europe/Paris');

// Include of configuration file
require_once dirname(__FILE__).'/config/config.php';

require_once WEBAPP_DIR.'/application/function/protectScript.php';
require_once WEBAPP_DIR.'/application/function/displayContentArray.php';

// Inclusion des classes et initialisation
require_once MODEL_DIR.'/Mysqli.php';
try{
	$oMysqliInstance = AYSMysqli::getInstance();
}catch(MysqliConnexionException $oException){
	$oException->addLog('MysqliConnexion');
	$sMessage = $oException->getMessage();
	include_once VIEW_DIR.'/errors.php';
	die();
}

set_include_path(
	'.'.
	PATH_SEPARATOR.
	MODEL_DIR.
	PATH_SEPARATOR.
	get_include_path()
);

require_once WEBAPP_DIR.'/application/Exceptions/ExceptionsList.php';

/* Display */
include_once CONTROLLER_DIR.'/headerController.php';

try{
	if(isset($_GET['page']) AND !empty($_GET['page'])){
		$sPage = $_GET['page' ];
		if(file_exists(CONTROLLER_DIR.'/'.$sPage.'Controller.php')){
			include_once CONTROLLER_DIR.'/'.$sPage.'Controller.php';
		}else{
			throw new PageNotFoundException(
				'Désolé, la page demandée n\'existe pas. L\'administrateur a été prévenu.',
				ERROR
			);
		}
	}else{
		include_once CONTROLLER_DIR.'/homeController.php';
		include_once CONTROLLER_DIR.'/newsController.php';
	}
}catch(PageNotFoundException $oException){
	$oException->addLog('PageNotFound');
	$sMessage = $oException->getMessage();
	include_once VIEW_DIR.'/errors.php';
}catch(Exception $oException){
	$oException->addLog('Exception');
	$sMessage = $oException->getMessage();
	include_once VIEW_DIR.'/errors.php';
}


include_once CONTROLLER_DIR.'/footerController.php';

// Closing current connexion with database
$oMysqliInstance->close();

ob_end_flush();
?>
